<?php
namespace Admin\Frontend\Controllers;

use Flow\Flow;
use Helpers\Hrm;

class FlowController extends ControllerBase
{

    public function getDepUserAction()
    {
        return $this->sendJsonResponse(Hrm::getFlowDepStaff());
    }

    /**
     * 根据流程标识获取流程列表
     *
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     *
     * create by ck 20171120
     */
    public function getFlowTemplateAction()
    {
        $tag = $this->request->getPost('tag', 'string');
        if (!$tag) {
            return $this->sendErrorResponse(400, '获取流程失败：缺少流程标识');
        }
        try {
            $flowInfo = (new Flow('flow'))->getFlowForNew($tag);
            return $this->sendJsonResponse($flowInfo);
        }
        catch (\Exception $e) {
            return $this->sendErrorResponse(500, '获取流程失败：' . $e->getMessage());
        }
    }

    /**
     * 获取提交流程节点
     *
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     *
     * create by ck 20171122
     */
    public function getFlowForCommitAction()
    {
        $moduleName = $this->request->getPost('moduleName', 'string');
        $tableName  = $this->request->getPost('tableName', 'string');
        $mainId     = $this->request->getPost('mainId', 'int');
        if (!$moduleName || !$mainId) {
            return $this->sendErrorResponse(400, '缺少流程必要参数');
        }
        try {
            $flowInfo = (new Flow($moduleName, $tableName))->getFlowForCommit($mainId);
            return $this->sendJsonResponse($flowInfo);
        }
        catch (\Exception $e) {
            return $this->sendErrorResponse(500, $e->getMessage());
        }
    }

    /**
     * 提交流程到下一节点
     *
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     *
     * create by ck 20171127
     */
    public function commitAction()
    {
        $moduleName = $this->request->getPost('moduleName', 'string');
        $tableName  = $this->request->getPost('tableName', 'string');
        $mainId     = $this->request->getPost('mainId', 'string');
        $code       = $this->request->getPost('code', 'int');
        $advice     = $this->request->getPost('advice', 'string');
        $nextCode   = $this->request->getPost('nextCode', 'int');
        $userIds    = $this->request->getPost('userIds', 'string');
        try {
            $result = (new Flow($moduleName, $tableName))->commit($mainId, $code, $advice, $nextCode, $userIds);
            if (!$result) {
                throw new \Exception('流程提交失败');
            }
            return $this->sendSuccessResponse('提交成功');
        }
        catch (\Exception $e) {
            return $this->sendErrorResponse(500, $e->getMessage());
        }
    }

    public function getPowerAction()
    {
        $moduleName = $this->request->getPost('moduleName', 'string');
        $tableName  = $this->request->getPost('tableName', 'string');
        $mainId     = $this->request->getPost('mainId', 'string');
        try {
            $result = (new Flow($moduleName, $tableName))->getPower($mainId);
            if (!$result) {
                throw new \Exception('流程提交失败');
            }
            return $this->sendJsonResponse($result);
        }
        catch (\Exception $e) {
            return $this->sendErrorResponse(500, $e->getMessage());
        }
    }

    public function getFlowAction()
    {
        $moduleName = $this->request->getPost('moduleName', 'string');
        $tableName  = $this->request->getPost('tableName', 'string');
        $mainId     = $this->request->getPost('mainId', 'string');
        try {
            $result = (new Flow($moduleName, $tableName))->getFlow($mainId);
            if (!$result) {
                throw new \Exception('流程提交失败');
            }
            return $this->sendJsonResponse($result);
        }
        catch (\Exception $e) {
            return $this->sendErrorResponse(500, $e->getMessage());
        }
    }

}